﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using WebTemplate.DAL.Repositories;
using WebTemplate.DAL.Entities;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Net.Mail;
using System.Net;



namespace WebTemplate.ControlPanel.Account
{
    public partial class SemesterList : System.Web.UI.Page
    {
        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                FillResult();
            }
        }

        protected void Semester_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.PageIndex = e.NewPageIndex;
            FillResult();
        }

        protected void Semester_Sorting(object sender, GridViewSortEventArgs e)
        {
            this.SortExpression = e.SortExpression;
            this.PageIndex = 0;
            FillResult();
        }

        protected void Semester_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteSemester")
            {
                int rowindex = int.Parse(e.CommandArgument.ToString());
                int SemesterID = (int)Semester.DataKeys[rowindex].Value;
                SemesterRepository repositroy = new SemesterRepository();
                repositroy.Delete(SemesterID);
                FillResult();
            }
        }

        #endregion

        #region

         private SemesterSearchCriteria SearchCriteria
            {
                get
                {
                    return this.ViewState["SearchCriteria"] as SemesterSearchCriteria;
                }

                set
                {
                    this.ViewState["SearchCriteria"] = value;
                }
            }
            private string SortExpression
            {
                set
                {
                    this.ViewState["SortExpression"] = value;
                }
                get
                {
                    return this.ViewState["SortExpression"] as string;
                }
            }

            private int PageIndex
            {
                set
                {
                    this.ViewState["PageIndex"] = value;
                }
                get
                {
                    return this.ViewState["PageIndex"] == null ? 0 : (int)this.ViewState["PageIndex"];
                }
            }

            private void FillResult()
            {
                var searchCriteria = this.SearchCriteria;
                var sortExpression = this.SortExpression;

                if (searchCriteria == null)
                {
                    searchCriteria = new SemesterSearchCriteria();
                    this.SearchCriteria = searchCriteria;
                }

                if (string.IsNullOrWhiteSpace(sortExpression))
                {
                    sortExpression = this.SortExpression = "ID";
                }
                SemesterRepository reprositry = new SemesterRepository();
                IEnumerable<Semester> result = reprositry.Select(searchCriteria);

                if (sortExpression == "Start")
                    result = result.OrderBy(x => x.StartDate).ToList();
                else if (sortExpression == "End")
                    result = result.OrderBy(x => x.EndDate).ToList();
                else if (sortExpression == "Name")
                    result = result.OrderBy(x => x.DisplayName).ToList();
                Semester.PageIndex = this.PageIndex;
                Semester.DataSource = result.ToList();
                Semester.DataBind();
            }

       #endregion

            protected void Add_Click(object sender, EventArgs e)
            {
                Response.Redirect("SemesterDetails.aspx");
            }
    }
}